//
//  LoanDetailsHeadView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/11/12.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit
import WebKit

@objc protocol LoanDetailsHeadViewDelegate {
    @objc optional func applyNowAction() //立即申请
}

class LoanDetailsHeadView: UIView {
    
    weak var delegate : LoanDetailsHeadViewDelegate?
    
    fileprivate let scrollView = UIScrollView()
    fileprivate let webView = WKWebView()
    fileprivate var textView : LoanDetailsTextView!
    fileprivate let lineView = UIView()
    
    fileprivate let applyButton = UIButton() //立即申请
    
    init(frame:CGRect,model:GenericLoansModel) {
        super.init(frame: frame)
        
        
        scrollView.showsVerticalScrollIndicator = false
        self.addSubview(scrollView)
        
        let titleView = LoanDetailsTitleView.init(model: model)
        scrollView.addSubview(titleView)
        
        textView = LoanDetailsTextView.init(model: model)
        scrollView.addSubview(textView)
        
        let grayView = UIView()
        grayView.backgroundColor = UIColor.verifyColor()
        scrollView.addSubview(grayView)
        
        let productLabel = UILabel()
        productLabel.text = productDetails
        productLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 18))
        scrollView.addSubview(productLabel)
        
        lineView.backgroundColor = UIColor.lineColor()
        scrollView.addSubview(lineView)
        
        var htmlString = "<html><head><meta name='viewport' content='initial-scale=1, maximum-scale=1, user-scalable=no'></head><body>\(model.details ?? "")</body></html>"
        if htmlString.contains("<img") {
            htmlString = htmlString.replacingOccurrences(of: "<img", with: "<img style='width:100%'")
        }
    
        webView.scrollView.bounces = false
        webView.navigationDelegate = self
        webView.loadHTMLString(htmlString, baseURL: URL.init(string: kmHttps))
        scrollView.addSubview(webView)
        
        //监听webview高度变化
        webView.scrollView.addObserver(self, forKeyPath: "contentSize", options: .new, context: nil)
        
        applyButton.backgroundColor = UIColor.red
        applyButton.setTitle(applyNow, for: .normal)
        applyButton.titleLabel?.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        applyButton.addTarget(self, action: #selector(applyNowAction), for: .touchUpInside)
        self.addSubview(applyButton)
        
        applyButton.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview().offset(-StatusBarAddNavBarHeight)
            make.height.equalTo(RATIO_H(maxNum: 45))
        }

        scrollView.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.bottom.equalTo(applyButton.snp.top)
        }

        titleView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(RATIO_H(maxNum: 20))
            make.left.equalTo(self).offset(RATIO(maxNum: 20))
            make.right.equalTo(self).offset(RATIO(maxNum: -20))
            make.height.equalTo(RATIO_H(maxNum: 130))
        }

        textView.snp.makeConstraints { (make) in
            make.top.equalTo(titleView.snp.bottom)
            make.left.right.equalTo(self)
            make.height.equalTo(RATIO_H(maxNum: 200))
        }

        grayView.snp.makeConstraints { (make) in
            make.top.equalTo(textView.snp.bottom)
            make.left.right.equalTo(self)
            make.height.equalTo(RATIO_H(maxNum: 10))
        }

        productLabel.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(RATIO(maxNum: 10))
            make.top.equalTo(grayView.snp.bottom).offset(RATIO_H(maxNum: 10))
            make.width.greaterThanOrEqualTo(10)
            make.height.greaterThanOrEqualTo(10)
        }

        lineView.snp.makeConstraints { (make) in
            make.top.equalTo(productLabel.snp.bottom).offset(RATIO_H(maxNum: 5))
            make.left.equalTo(productLabel)
            make.right.equalTo(self).offset(RATIO(maxNum: -10))
            make.height.equalTo(1)
        }

        webView.snp.makeConstraints { (make) in
            make.top.equalTo(lineView.snp.bottom).offset(RATIO_H(maxNum: 10))
            make.left.right.equalTo(self)
            make.bottom.equalToSuperview()
        }
        
        
    }
    
    //MARK: 立即申请
    @objc fileprivate func applyNowAction() {
        
        self.delegate?.applyNowAction!()
    }
    
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        
        if keyPath == "contentSize" {
            
            webView.snp.remakeConstraints { (make) in
                make.top.equalTo(self.lineView.snp.bottom).offset(RATIO_H(maxNum: 10))
                make.left.right.equalTo(self)
                make.height.equalTo(webView.scrollView.contentSize.height)
            }

            self.layoutIfNeeded()

            self.scrollView.contentSize = .init(width: ScreenW, height: webView.frame.maxY)
        
            
        }
    }
    
    deinit {
        webView.scrollView.removeObserver(self, forKeyPath: "contentSize")
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension LoanDetailsHeadView : WKNavigationDelegate {
    
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        
       
    }
    
    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
        
        
        
    }
}


